package com.zjt.pictureselector.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.zjt.pictureselector.R;
import com.zjt.pictureselector.holder.PictureHolder;
import com.zjt.pictureselector.model.MediaBean;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2017</p>
 * <P>Company: </p>
 *
 * @author zhengjianting
 * @time 2017/11/9 0009 上午 11:17
 */

public class PictureAdapter extends RecyclerView.Adapter<PictureHolder> {

    private Context mContext;
    private List<MediaBean> mediaBeanList;
    private OnItemSelectListener itemSelectListener;
    private List<MediaBean> selectList = new ArrayList<>();
    private int mSelectNum;
    private int mMaxSelectNum;

    public PictureAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public PictureAdapter(Context context, List<MediaBean> mediaBeanList) {
        this.mediaBeanList = mediaBeanList;
        this.mContext = context;
    }

    @Override
    public PictureHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PictureHolder(LayoutInflater.from(mContext), parent);
    }

    @Override
    public void onBindViewHolder(final PictureHolder holder, int position) {
        final MediaBean mediaBean = mediaBeanList.get(position);

        String imagePath = mediaBean.getPath();
        if (mediaBean.getType() == MediaBean.Type.Video) {
            imagePath = mediaBean.getThumbPath();
        }

        Glide.with(mContext)
                .load(imagePath)
                .placeholder(R.mipmap.icon_placeholder)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(holder.getImageView());

        if (mediaBean.getType() == MediaBean.Type.Video) {
            holder.getVideoPlay().setVisibility(View.VISIBLE);
            holder.getVideoDuration().setVisibility(View.VISIBLE);
            holder.getVideoDuration().setText(formatTime(mediaBean.getDuration()));
        } else {
            holder.getVideoPlay().setVisibility(View.GONE);
            holder.getVideoDuration().setVisibility(View.GONE);
        }
        holder.getImgSelect().setSelected(mediaBean.isSelect());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaBean.isSelect()) {
                    holder.getImgSelect().setSelected(false);
                    unSelectMedia(mediaBean);
                } else {
                    if (getSelectNum() >= mMaxSelectNum) {
                        Toast.makeText(mContext, "已经选了9张图", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    holder.getImgSelect().setSelected(true);
                    selectMedia(mediaBean);
                }
                itemSelectListener.onClick(getSelectNum());
            }
        });
    }

    private String formatTime(int duration){
        String time = "";
        int second = (duration / 1000) % 60;
        int minute = (duration / 1000) / 60;
        if (minute < 10) {
            time = "0";
        }
        time += minute;
        if (second < 10){
            time += ":0" + second;
        } else {
            time += ":" + second;
        }
        return time;
    }

    @Override
    public int getItemCount() {
        return mediaBeanList == null ? 0 : mediaBeanList.size();
    }

    public void refreshData(List<MediaBean> data){
        mediaBeanList = data;
        notifyDataSetChanged();
    }

    public void setMaxSelectNum(int mMaxSelectNum) {
        this.mMaxSelectNum = mMaxSelectNum;
    }

    private int getSelectNum() {
        return mSelectNum;
    }

    public void selectMedia(MediaBean mediaBean){
        if (selectList.contains(mediaBean)) return;
        mediaBean.setSelect(true);
        mSelectNum++;
        selectList.add(mediaBean);
    }

    private void unSelectMedia(MediaBean mediaBean){
        if (selectList.isEmpty()) return;
        mediaBean.setSelect(false);
        mSelectNum--;
        selectList.remove(mediaBean);
    }

    public List<MediaBean> getSelectList() {
        return selectList;
    }

    public void setItemSelectListener(OnItemSelectListener itemSelectListener) {
        this.itemSelectListener = itemSelectListener;
    }

    public interface OnItemSelectListener{
        void onClick(int num);
    }
}
